package springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;


import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.Collections;
import java.util.List;
/**
 * @author daiqiang
 * @since 2024/6/4-9:43
 * 1.接口  ApiInfo apiInfo()
 * 2.获取统一授权码  .securitySchemes(Collections.singletonList(securityScheme()))
 *              .securityContexts(Collections.singletonList(securityContext()));
 */
@Configuration
@EnableOpenApi
@EnableWebMvc

public class MySwaggerConfig {
    @Bean
    public Docket api(){
        return new Docket(DocumentationType.OAS_30)
                 .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("springboot"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(Collections.singletonList(securityScheme()))
                .securityContexts(Collections.singletonList(securityContext()));
    }

    private SecurityScheme securityScheme(){
      //return new ApiKey("Authorization","Authorization", "header");
    return new ApiKey("Token","Token","header");
    }
    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("^(?!auth).*$"))
                .build();
    }
    private List<SecurityReference> defaultAuth(){
        AuthorizationScope authorizationScope = new AuthorizationScope("global","accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0]=authorizationScope;return Collections.singletonList(
            new SecurityReference("Token",authorizationScopes));
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("个人博客管理系统接口文档")
                .description("SpringBoot+Vue前后端分离项目实战")
                .version("1.0@")
                .contact(new Contact("qqcn", "http://www.qqcn.cn","1871174426@qq.com"))
                .build();

    }
}

